package com.henu.service;

import java.util.List;
import java.util.Map;

import org.smart4j.framework.dao.DatabaseHelper;
import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.tx.annotation.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

@Service
public class OrderInfoService {
    @Inject
    private UserLoginService userLoginService;

    public boolean createOrder(Integer userID, String name, String address, String zcode, String phone,
            String productIDlist) {
        Long returnID = (Long) DatabaseHelper
                .insertReturnPK(
                        "insert into porder(name,phone,address,zcode,userID,createTime,status) values(?,?,?,?,?,sysdate(),'0')",
                        name, phone,
                        address, zcode, userID);
        JSONArray list=JSON.parseArray(productIDlist);
        for(Object o:list)
        {
            DatabaseHelper.update("insert into orderinfo(orderID,productID,status) values(?,?,1)", returnID,
                    o.toString()
                    );
        }
        return true;
    }
    
    /**
     * 获取最新的订单，未发货的，或者已发货的
     * @param userID
     * @return
     */
    public List getNewOrder(Integer userID) {
        String sql = "select p.imageUri IamgeUri,p.`name` Name,p.id ID,p.price Price,o.`status` Status,t.id UID from porder t ,orderinfo o,productinfo p where t.id=o.orderID and o.productID=p.id and o.`status` in(0,1) and t.userID=?";
        return DatabaseHelper.queryMapList(sql, userID);
    }

    /**
     * 获取历史订单，已收货的，跟已评价的
     * @param userID
     * @return
     */
    public List getHistoryOrder(Integer userID) {
        String sql = "select p.imageUri \"IamgeUri\",p.`name` \"Name\",p.id \"ID\",p.price \"Price\",o.`status` \"Status\",t.id \"UID\" from porder t ,orderinfo o,productinfo p where t.id=o.orderID and o.productID=p.id and o.`status` in(2,3) and t.userID=?";
        return DatabaseHelper.queryMapList(sql, userID);
    }

    /**
     * 确认收货
     * @param productID
     * @param orderID
     * @return
     */
    public boolean sendProduct(Integer productID, Integer orderID) {
        String sql = "update orderinfo o set o.`status`=2 where o.orderID=? and o.productID=? ";
        return DatabaseHelper.update(sql, orderID, productID) == 0;
    }

    /**
     * 商品评价
     * @param productID
     * @param userID
     * @param star
     * @param comment
     * @return
     */
    public boolean productAppraise(Integer OrderID, Integer productID, Integer userID, float star, String comment) {
        Map userBean = userLoginService.queryUserById(userID);
        String sql = "insert into appraise(orderID,productID,userName,userID,time,comment,star) values(?,?,?,?,sysdate(),?,?)";
        String sql1 = "update orderinfo o set o.`status`=3 where o.orderID=? and o.productID=?";
        boolean flag1 = DatabaseHelper.update(sql, OrderID, productID, userBean.get("userName"), userID, comment, star) != 0;
        boolean flag2 = DatabaseHelper.update(sql1, OrderID, productID) != 0;
        return flag1 && flag2;
    }
}
